package com.growthgate.bootcommon.util;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring工具类
 *
 * @author GrowthGate
 * @date 2019/11/21
 */
@Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }

    @NotNull
    public static <T> T getBean(@NotNull Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    @NotNull
    public static <T> T getBean(@NotNull String name, @NotNull Class<T> clazz) {
        return applicationContext.getBean(name, clazz);
    }

    @NotNull
    public static String[] getBeanNamesForType(@Nullable Class<?> clazz) {
        return applicationContext.getBeanNamesForType(clazz);
    }

}
